﻿// CheckCaseLibraryDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "CheckCaseLibraryDialog.h"
#include "afxdialogex.h"

#include "CheckCaseNewDialog.h"
#include "CheckCaseEditDialog.h"


// CCheckCaseLibraryDialog 对话框

IMPLEMENT_DYNAMIC(CCheckCaseLibraryDialog, CDialogEx)

CCheckCaseLibraryDialog::CCheckCaseLibraryDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CHECK_CASE_LIBRARY, pParent)
{

}

CCheckCaseLibraryDialog::~CCheckCaseLibraryDialog()
{
}

void CCheckCaseLibraryDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_CASE, m_list);
	DDX_Control(pDX, IDC_MFCPROPERTYGRID1, m_props);
}


BEGIN_MESSAGE_MAP(CCheckCaseLibraryDialog, CDialogEx)
	ON_COMMAND(ID_CHECK_CASE_NEW, &CCheckCaseLibraryDialog::OnCheckCaseNew)
	ON_UPDATE_COMMAND_UI(ID_CHECK_CASE_NEW, &CCheckCaseLibraryDialog::OnUpdateCheckCaseNew)
	ON_COMMAND(ID_CHECK_CASE_REMOVE, &CCheckCaseLibraryDialog::OnCheckCaseRemove)
	ON_UPDATE_COMMAND_UI(ID_CHECK_CASE_REMOVE, &CCheckCaseLibraryDialog::OnUpdateCheckCaseRemove)
	ON_COMMAND(ID_CHECK_CASE_EDIT, &CCheckCaseLibraryDialog::OnCheckCaseEdit)
	ON_UPDATE_COMMAND_UI(ID_CHECK_CASE_EDIT, &CCheckCaseLibraryDialog::OnUpdateCheckCaseEdit)
	ON_NOTIFY(NM_CLICK, IDC_LIST_CASE, &CCheckCaseLibraryDialog::OnNMClickListCase)
END_MESSAGE_MAP()


// CCheckCaseLibraryDialog 消息处理程序


BOOL CCheckCaseLibraryDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	CBitmap bmp;
	if (bmp.LoadBitmap(IDB_BITMAP_CASE_LIST_32))
	{
		m_il.Create(32, 32, ILC_COLOR32 | ILC_MASK, 0, 0);
		m_il.Add(&bmp, RGB(255, 0, 255));
		int nCount = m_il.GetImageCount();
		for (int i = 1; i < nCount; i++)         //support 16
		{
			m_il.SetOverlayImage(i, i);
		}
		m_list.SetImageList(&m_il, TVSIL_NORMAL);
	}

	//m_list.Populate();

	CRect rect;
	GetDlgItem(IDC_STATIC_BORDER)->GetWindowRect(rect);
	ScreenToClient(rect);
	int margin = 1;
	rect.left += margin;
	rect.right -= margin;
	rect.top += margin;
	rect.bottom -= margin;
	m_props.MoveWindow(rect);

	m_list.Populate();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CCheckCaseLibraryDialog::OnCheckCaseNew()
{
	// TODO: 在此添加命令处理程序代码
	CCheckCaseNewDialog dlg;
	if (dlg.DoModal() == IDOK)
	{
		m_list.Populate();
	}
}


void CCheckCaseLibraryDialog::OnUpdateCheckCaseNew(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码

	
}


void CCheckCaseLibraryDialog::OnCheckCaseRemove()
{
	// TODO: 在此添加命令处理程序代码
	POSITION pos = m_list.GetFirstSelectedItemPosition();
	if (pos)
	{
		int nItem = m_list.GetNextSelectedItem(pos);
		CString strCaseName = m_list.GetItemText(nItem, 0);

		ck::CheckCase* pCase = NULL;
		ck::CheckCaseManager* pCaseManager = ck::CheckCaseManager::GetInstance();
		pCase = pCaseManager->GetCase(strCaseName);
		if (pCase)
		{
			z_uint32 nCaseCode = pCase->GetCode();
			
			RESULTCODE rc = pCaseManager->DeleteCase(nCaseCode);
			if (rc==RC_SUCCESS)
			{
				m_list.DeleteItem(nItem);
			}
			else
			{
				CString strText;
				strText.Format("删除方案[%s]失败", pCase->GetName());
				AfxMessageBox(strText);
			}

			pCase->Release();
		}

	}
}


void CCheckCaseLibraryDialog::OnUpdateCheckCaseRemove(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CCheckCaseLibraryDialog::OnCheckCaseEdit()
{
	// TODO: 在此添加命令处理程序代码

	POSITION pos = m_list.GetFirstSelectedItemPosition();
	if (pos)
	{
		int nItem = m_list.GetNextSelectedItem(pos);
		CString strCaseName = m_list.GetItemText(nItem, 0);

		ck::CheckCase* pCase = NULL;
		ck::CheckCaseManager* pCaseManager = ck::CheckCaseManager::GetInstance();
		pCase = pCaseManager->GetCase(strCaseName);
		if (pCase)
		{
			CCheckCaseEditDialog dlg;
			dlg.SetCheckCase(pCase);
			dlg.DoModal();

			pCase->Release();
		}

		
	}
}


void CCheckCaseLibraryDialog::OnUpdateCheckCaseEdit(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}



void CCheckCaseLibraryDialog::OnNMClickListCase(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	int nItem = pNMItemActivate->iItem;
	if (nItem < 0)
	{
		return;
	}
	

	z_uint32 code = (z_uint32)m_list.GetItemData(nItem);
	CString strModName = m_list.GetItemText(nItem, 0);

	ck::CheckCase* pCase = NULL;
	ck::CheckCaseManager* pCaseManager = ck::CheckCaseManager::GetInstance();
	pCase = pCaseManager->GetCase(strModName);
	if (pCase)
	{
		m_props.Populate(pCase);
		pCase->Release();
	}

	*pResult = 0;
}
